import { Request, Response } from "express";
import { BlogService } from "../services/blog.service";
import { parse } from "path";

export class BlogController {
  private blogService: any;
  constructor() {
    this.blogService = new BlogService();
  }

  getAllBlogs = async (req: Request, res: Response) => {
    const page = parseInt(req.query.page as string) || 1;
    const pageSize = parseInt(req.query.pageSize as string) || 5;
    const keyword = (req.query.search as string) || null;
    const data = await this.blogService.getAllBlogs(page, pageSize, keyword);
    return res.status(200).json({
      code: 200,
      success: true,
      messasge: "Blogs retrieved successfully",
      data: {
        data: data.blogs,
        total: data.total,
        page: page,
        pageSize: pageSize,
      },
    });
  };
  getBlog = async (req: Request, res: Response) => {
    const blogId = parseInt(req.params.id as string);
    const blog = await this.blogService.getBlogById(blogId);
    if (!blog) {
      return res.status(1001).json({
        code: 1001,
        success: false,
        message: "Blog not found",
        data: null,
      });
    }
    return res.status(200).json({
      code: 200,
      success: true,
      messasge: "Blog retrieved successfully",
      data: blog,
    });
  };
  createBlog = async (req: Request, res: Response) => {
    const blog = req.body;
    const user_id = req.session.user?.userId;
    const createdBlog = await this.blogService.createBlog({ ...blog, user_id });
    return res.status(200).json({
      code: 200,
      success: true,
      messasge: "Blog created successfully",
      data: createdBlog,
    });
  };
  updateBlog = async (req: Request, res: Response) => {
    const blogId = req.params.id;
    const blog = req.body;
    const oldBlog = await this.blogService.getBlogById(blogId);
    if (!oldBlog) {
      return res.status(1001).json({
        code: 1001,
        success: false,
        message: "Blog not found",
        data: null,
      });
    }
    const needBlog = Object.assign(oldBlog, blog);
    const updatedBlog = await this.blogService.updateBlog(oldBlog.id, needBlog);
    return res.status(200).json({
      code: 200,
      success: true,
      messasge: "Blog updated successfully",
      data: updatedBlog,
    });
  };
  deleteBlog = async (req: Request, res: Response) => {
    const blogId = parseInt(req.params.id as string);
    await this.blogService.deleteBlog(blogId);
    return res.status(200).json({
      code: 200,
      success: true,
      messasge: "Blog deleted successfully",
      data: null,
    });
  };
  getUserBlogs = async (req: Request, res: Response) => {
    const userId = parseInt(req.params.userId as string);
    const page = parseInt(req.query.page as string) || 1;
    const pageSize = parseInt(req.query.pageSize as string) || 5;

    const { blogs, total } = await this.blogService.getBlogByAuthorId(
      userId,
      page,
      pageSize
    );
    if (!blogs) {
      return res.status(1001).json({
        code: 1001,
        success: false,
        message: "Blogs not found",
        data: null,
      });
    }
    return res.status(200).json({
      code: 200,
      success: true,
      messasge: "Blogs retrieved successfully",
      data: {
        data: blogs,
        total: total,
        page: page,
        pageSize: pageSize,
      },
    });
  };
}
